package com.clkj.sunhelp.mvp.login;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.clkj.sunhelp.R;
import com.clkj.sunhelp.app.CustomApplication;
import com.clkj.sunhelp.bean.UserLoginInfo;
import com.clkj.sunhelp.bean.WorkerLoginInfo;
import com.clkj.sunhelp.common.Config;
import com.clkj.sunhelp.common.SharePreKey;
import com.clkj.sunhelp.common.TipStr;
import com.clkj.sunhelp.mvp.base.BaseActivity;
import com.clkj.sunhelp.mvp.register.ActivityRegister;
import com.clkj.sunhelp.util.CommonUtil;
import com.clkj.sunhelp.util.IntentUtil;
import com.clkj.sunhelp.util.ToastUtil;
import com.clkj.sunhelp.util.schedulers.SchedulerProvider;
import com.clkj.sunhelp.widget.ClearEditText;
import com.jakewharton.rxbinding.view.RxView;

import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by Administrator on 2017/3/27.
 */

public class ActivityLogin extends BaseActivity implements LoginContract.View {


    private RadioGroup rgChooseRole;
    private RadioButton rbRolePeople;
    private RadioButton rbRoleWorker;
    private ClearEditText accountet;
    private ClearEditText passwordet;
    private TextView logintv;
    private TextView forgetpasstv;
    private TextView registertv;


    //角色是否是普通用户
    boolean isRolePeople;

    LoginContract.Presenter mPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initTitleWithRightTvOrIv(null, null, "登录", true, null, null);
        initData();
        assignView();
        initView();

        mPresenter = new LoginPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    @Override
    public void initData() {
        isRolePeople = false;
    }

    @Override
    public void assignView() {
        rgChooseRole = (RadioGroup) findViewById(R.id.rg_choose_role);
        rbRolePeople = (RadioButton) findViewById(R.id.rb_role_people);
        rbRoleWorker = (RadioButton) findViewById(R.id.rb_role_worker);
        accountet = (ClearEditText) findViewById(R.id.accountet);
        passwordet = (ClearEditText) findViewById(R.id.passwordet);
        logintv = (TextView) findViewById(R.id.logintv);
        forgetpasstv = (TextView) findViewById(R.id.forgetpasstv);
        registertv = (TextView) findViewById(R.id.registertv);
    }

    @Override
    public void initView() {
        rgChooseRole.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb_role_people:
                        isRolePeople = true;
                        break;
                    case R.id.rb_role_worker:
                        isRolePeople = false;
                        break;
                }
            }
        });


        RxView.clicks(registertv)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        IntentUtil.startNewActivity(ActivityLogin.this, ActivityRegister.class);
                    }
                });

        RxView.clicks(logintv)
                .filter(new Func1<Void, Boolean>() {
                    @Override
                    public Boolean call(Void aVoid) {
                        boolean result = false;
                        if (CommonUtil.isStringEmpty(accountet.getText().toString(), "请输入账号") == false
                                && CommonUtil.isStringEmpty(passwordet.getText().toString(), "请输入密码") == false) {
                            result = true;
                        }

                        return result;
                    }
                }).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                if (isRolePeople) {
                    loginPeople();
                } else {
                    loginWorker();
                }
            }
        });
    }

    @Override
    public void loginPeople() {
        mPresenter.loginPeople(accountet.getText().toString(), passwordet.getText().toString());
    }

    @Override
    public void onLoginPeopleSuccess(UserLoginInfo userLoginInfo) {
        CommonUtil.putShareValue(SharePreKey.SP_KEY_USERID,userLoginInfo.getId());
        CommonUtil.putShareValue(SharePreKey.SP_KEY_USERNAME,userLoginInfo.getUserName());
        CommonUtil.putShareValue(SharePreKey.SP_KEY_REALNAME,userLoginInfo.getRealName());
        CommonUtil.putShareValue(SharePreKey.SP_KEY_CONTACT_TEL,userLoginInfo.getLxfs());
        CommonUtil.putShareValue(SharePreKey.SP_KEY_USER_ROLE, Config.TYPE_ROLE_PEOPLE);
        CommonUtil.putShareValue(SharePreKey.SP_KEY_LOGIN_TAG, true);
        ToastUtil.showShort(this, TipStr.TIP_LOGIN_SUCCESS);
        finish();

    }

    @Override
    public void onLoginPeopleError(String tip) {
        ToastUtil.showShort(this, tip);
    }

    @Override
    public void loginWorker() {
        mPresenter.loginWorker(accountet.getText().toString(), passwordet.getText().toString());
    }

    @Override
    public void onLoginWorkerSuccess(WorkerLoginInfo workerLoginInfo) {

        CommonUtil.putShareValue(SharePreKey.SP_KEY_USERID,workerLoginInfo.getId());
        CommonUtil.putShareValue(SharePreKey.SP_KEY_USERNAME,workerLoginInfo.getUserName());
        CommonUtil.putShareValue(SharePreKey.SP_KEY_REALNAME,workerLoginInfo.getRealName());
        if (TextUtils.isEmpty(workerLoginInfo.getMobile())==false)
        {
            CommonUtil.putShareValue(SharePreKey.SP_KEY_CONTACT_TEL,workerLoginInfo.getMobile());
        }else {
            CommonUtil.putShareValue(SharePreKey.SP_KEY_CONTACT_TEL,"");
        }

        CommonUtil.putShareValue(SharePreKey.SP_KEY_USER_ROLE, Config.TYPE_ROLE_WORKER);
        CommonUtil.putShareValue(SharePreKey.SP_KEY_LOGIN_TAG, true);
        //CommonUtil.putShareValue(SharePreKey.SP_KEY_WORK_ROLE, workerLoginInfo.getRoleName());
        ToastUtil.showShort(this, TipStr.TIP_LOGIN_SUCCESS);

        finish();
    }

    @Override
    public void onLoginWorkerError(String tip) {
        ToastUtil.showShort(this, tip);
    }

    @Override
    public void setPresenter(LoginContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {
        showProgressDialog(false, null);
    }

    @Override
    public void dismissLoading() {
        dismissProgressDialog();
    }
}
